/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.fields.checkbox
{
	import flash.events.MouseEvent;
	import net.babeuf.form.fields.AbstractField;
	import net.babeuf.form.fields.IField;
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class CheckBoxField extends AbstractField implements IField 
	{
		protected var _checked : Boolean ; 
		protected var _default : Boolean ; 	
		
		protected var _valueChecked : * ; 
		protected var _valueUnchecked : * ; 
		
		
		
		public function CheckBoxField( state : Boolean = false ) 
		{
			_default = _checked = state ;
			super() ; 
		}
		
		override public function dispose():void 
		{
			super.dispose();
			_valueChecked = null ; 
			_valueUnchecked = null ; 
		}
		
		override public function empty() : void 
		{
			_checked = _default ; 
			_repaint() ; 
		}
		
		override public function getValue() : *
		{
			if ( _checked && ( _valueChecked != null ) ) return _valueChecked ; 
			if (  !_checked && ( _valueUnchecked != null ) ) return _valueUnchecked ; 			
			return _checked ; 
		}
		
		
		public function defineValues( checked : * = null, unchecked : * = null ) : void
		{
			_valueChecked = checked ;
			_valueUnchecked = unchecked ;
		}
		
		
		override public function setValue( value : * ) : void
		{
			_checked = Boolean(value) ; 
			_repaint() ; 
		}
		
		public function get selected() : Boolean
		{
			return _checked ; 
		}
		
		override public function validate() : Boolean 
		{		
			super.validate();
			var ok : Boolean = optional ? true :  _checked ;
			if ( !ok )_error = _emptyError ; 
			if ( _error != "" ) {_updateSensors(false) ; return false ; }
			else return true ; 
		}
		
		protected function _repaint() : void
		{
			
		}
		
		
		
		protected function _toggle( e:  MouseEvent = null ) : void
		{
			_checked = !_checked ; 
			_repaint()  ;
			_notifyChanged() ; 
		}
		
		
		
	}

}